package logic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"mall/mallrpc/internal/svc"
	"mall/mallrpc/mallrpc"
	"mall/model/mysql"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加购物车
func (l *AddLogic) Add(in *mallrpc.AddRequest) (*mallrpc.AddShResponse, error) {
	// todo: add your logic here and delete this line
	shopcar := mysql.Shopcar{
		Shop:  in.Shop,
		Num:   int(in.Num),
		Price: in.Price,
	}
	mysql.DB.Debug().Where("shop=?", in.Shop).Find(&shopcar)
	if shopcar.ID != 0 {
		return nil, fmt.Errorf("商品不能重复添加")
	}
	if shopcar.Num > 100 {
		return nil, fmt.Errorf("商品不能超卖100件")
	}
	mysql.DB.Create(&shopcar)
	return &mallrpc.AddShResponse{
		Id: int64(shopcar.ID),
	}, nil
}
